/*
 * 下分按钮
 * author: sfmy
 */
var SGModel           = require('./SGModel');
var SGProto           = require('./SGProto');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,
	properties: {
		pourButtonLabel: cc.Label,
		sliderBar: cc.Slider,
		progressBar: cc.ProgressBar,
	},

	onLoad: function () {
		this.node.active = false;
		Global.MessageCallback.addListener('RoomMessagePush', this);
		Global.MessageCallback.addListener('GameMessagePush', this);
		Global.MessageCallback.addListener('ReConnectSuccess', this);
	},

	gameInit () {
		let canPourScores = SGModel.getCanPourScores();
		for (let i = 0; i < canPourScores.length; ++i) {
			let node = cc.find('Layout/button'+i, this.node);
			if (node) {
				cc.find('Label', node).getComponent(cc.Label).string = canPourScores[i]+'分';
				node.active = true;
			}
		}
		for (let i = canPourScores.length; i <= 4; ++i) {
			let node = cc.find('Layout/button'+i, this.node);
			if (node) {
				node.active = false;
			}
		}
		this.pourButtonLabel.string = canPourScores[0]+'分';
		this.sliderBar.progress = 0;
		this.progressBar.progress = 0;
		if (SGModel.getGameStatus() == SGProto.gameStatus.POUR_SCORE && SGModel.getMyChairID() != SGModel.getBankerChairID() && SGModel.getPourScoreByChairID(SGModel.getMyChairID()) == 0 && SGModel.getIsPlayingByChairID(SGModel.getMyChairID())) {
			this.node.active = true;
		}
		else {
			this.node.active = false;
		}
		/* TODO 资源替换 */
	},

	messageCallbackHandler: function(router, msg) {
		if (!SGModel.getGameInited()) { return; }
		if (router === 'RoomMessagePush') {
			if(msg.type === RoomProto.GET_ROOM_SCENE_INFO_PUSH) {
				this.gameInit();
			}
		}
		else if(router === 'GameMessagePush') {
			if(msg.type === SGProto.GAME_STATUS_PUSH) {
				let myID = SGModel.getMyChairID();
				let bankerID = SGModel.getBankerChairID();
				if ((msg.data.gameStatus == SGProto.gameStatus.POUR_SCORE) && (SGModel.getMyChairID() != SGModel.getBankerChairID())) {
					let delay = 0;
					if (SGModel.getGameType() == SGProto.gameType.MINGPAIQZ || SGModel.getGameType() == SGProto.gameType.ZHIYOUQZ) {
						delay = 2.5;
					}
					this.scheduleOnce(() => {
						if ((SGModel.getGameStatus() == SGProto.gameStatus.POUR_SCORE) && (SGModel.getMyChairID() != SGModel.getBankerChairID())) {
							this.gameInit();
						}
					}, delay);
				}
				else {
					this.node.active = false;
				}
			}
			else if(msg.type === SGProto.GAME_POUR_SCORE_PUSH) {
				if (msg.data.chairID == SGModel.getMyChairID()) {
					this.node.active = false;
				}
			}
		} 
	},

	onButtonClick: function (event, param) {
        Global.AudioManager.playCommonSoundClickButton();
		let canPourScores = SGModel.getCanPourScores();
		let score = canPourScores[parseInt(param)];
		let sendData = SGProto.gamePourScoreNotify(score);
		Global.NetworkManager.notify(GameMessageRouter, sendData);
		this.node.active = false;
	},

	onPourClick: function () {
        Global.AudioManager.playCommonSoundClickButton();
		let sendData = SGProto.gamePourScoreNotify(this.getProgressScore());
		Global.NetworkManager.notify(GameMessageRouter, sendData);
		this.node.active = false;
	},

	onSlider: function (event, param) {
		this.progressBar.progress = event.progress;
		this.pourButtonLabel.string = this.getProgressScore() + '分';
	},

	onDestroy: function () {
		Global.MessageCallback.removeListener('RoomMessagePush', this);
		Global.MessageCallback.removeListener('GameMessagePush', this);
		Global.MessageCallback.removeListener('ReConnectSuccess', this);
	},

	getProgressScore: function () {
		let canPourScores = SGModel.getCanPourScores();
		let maxCanPourScore = SGModel.getMaxCanPourScore();
		let score = canPourScores[0] + Math.floor((maxCanPourScore-canPourScores[0])*this.progressBar.progress);
		return score;
	},
});
